// pages/my/my.js
var WxNotificationCenter = require("../../utils/WxNotificationCenter.js");
var MenuDates = require("../../utils/data.js");
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    hasUserInfo: false,
    //canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfoX: '',
    confirmShow: false,
    orderNum01: 0,
    orderNum02: 0,
    orderNum03: 0,
    phoneValidate: true,
    menu: MenuDates.getMenuDates()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    //用户登陆
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
    
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   console.log("显示")
    if (getApp().appData.userInfo) {
      this.getOrderNum()
    } else {
      this.setData({
        orderNum01: 0,
        orderNum02: 0,
        orderNum03: 0,
      })
    }
      this.getUser()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function () {

  // },

  // /**
  //  * 页面上拉触底事件的处理函数
  //  */
  // onReachBottom: function () {

  // },


  /**
   * 手机认证
   */
  phoneValidateTap() {
    wx.navigateTo({
      url: '/pages/phoneVerification/phoneVerification',
    })
  },
  /**
   * 关闭认证
   */
  closeTap() {
    this.setData({
      renZheng: false
    })
  },

  /**
   * 获取订单数量
   */
  getOrderNum() {
    var that = this
    var url = getApp().appData.host + "GetUserList"
    var parm = ""
    var token = getApp().appData.userInfo.token
    var mod = "statenum"
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      data: {
        "parm": parm,
        "mod": mod,
        "token": token
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'POST',
      success: function(res) {
        console.log("获取订单数量", res.data)
        //请求成功
        if (res.data.state == 1) {
          var list = res.data.data
          var orderNum01 = 0;
          var orderNum02 = 0;
          var orderNum03 = 0;
          for (var i = 0; i < list.length; i++) {
            var key = list[i].Key
            var value = list[i].Value
            if (key == 0) {
              orderNum01 = value
            } else if (key == 2) {
              orderNum02 = value
            } else if (key == 3) {
              orderNum03 = value
            }
          }
          that.setData({
            orderNum01: orderNum01,
            orderNum02: orderNum02,
            orderNum03: orderNum03,
          })
        } else { //请求失败      

        }
      },
      fail: function(res) {

      },
      complete: function() {

      }
    })
  },

  yiJianFanKui: function() {
    if (getApp().appData.userInfo) {
      wx.navigateTo({
        url: '/pages/feedback/feedback'
      })
    } else {
      this.loginTap()
    }
  },

  searchlink: function() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  contactService: function() {
    wx.makePhoneCall({
      phoneNumber: '400-100-9093'
    })
  },

  loginTap: function() {
    if (getApp().appData.userInfo) {

    } else {
      this.setData({
        confirmShow: true
      })
      WxNotificationCenter.addNotification(getApp().appData.notifyUserInfo, this.getUserInfo, this)
    }
  },

  getUserInfo: function(e) {
    console.log("收到广播", e)
    var that = this
    this.setData({
      userInfo: JSON.parse(e)
    })
    WxNotificationCenter.removeNotification(getApp().appData.notifyUserInfo, this)
    that.setData({
      userInfoX: JSON.parse(e)
    })
    that.getOrderNum()
    that.getCartNum()
  },
  /**
   * 跳转订单页
   */
  orderList: function(e) {
    // console.log(e.currentTarget.dataset.selectedid);
    if (getApp().appData.userInfo) {
      wx.navigateTo({
        url: '/pages/myOrder/myOrder?selectedId=' + e.currentTarget.dataset.selectedid,
      })
    } else {
      this.loginTap()
    }
  },
  /**
   * 跳转账单页
   */
  woDeZhangDan: function(e) {
    // console.log(e.currentTarget.dataset.selectedid);
    if (getApp().appData.userInfo) {
      wx.navigateTo({
        url: '/pages/myBill/myBill',
      })
    } else {
      this.loginTap()
    }
  },
  /**
   * 跳转我的关注
   */
  woDeGuanZhu: function(e) {
    // console.log(e.currentTarget.dataset.selectedid);
    if (getApp().appData.userInfo) {
      wx.navigateTo({
        url: '/pages/myFocus/myFocus',
      })
    } else {
      this.loginTap()
    }
  },
  /**
   * 跳转收货地址
   */
  shouHuoDiZhi: function(e) {
    // console.log(e.currentTarget.dataset.selectedid);
    if (getApp().appData.userInfo) {
      wx.navigateTo({
        url: '/pages/myAddress/myAddress',
      })
    } else {
      this.loginTap()
    }
  },
  /**
   * 扫描二维码
   */
  scanTap() {
    wx.scanCode({
      success: (res) => {
        var result = res.result
        console.log(result)
        var reg01 = RegExp(/http:\/\/www.stone2.com\//);
        var reg02 = RegExp(/http:\/\/www.shi.cn/);
        var reg03 = RegExp(/stoneo2o.com/);
        if (reg01.test(result)) {
          wx.showModal({
            title: '提示',
            content: '出入库暂不支持二维码扫描，请下载百石商城app扫描',
            showCancel: false
          })
          // console.log("出入库")
          // result = result.replace(".html","")
          // result = result.substring(22)
          // var that = this
          // wx.showLoading({
          //   title: '加载中……',
          //   mask: true
          // })
          // wx.request({
          //   url: "http://www.stone2.com/qrlabel/shibie?id=" + result, //仅为示例，并非真实的接口地址
          //   data: {

          //   },
          //   header: {
          //     'content-type': 'application/json', // 默认值
          //   },
          //   method: 'POST',
          //   success: function (res) {
          //     console.log("出入库", res.data)
          //     //请求成功
          //     if (res.data.state == 1) {

          //       wx.hideLoading()
          //       that.setData({
          //         isfail: false,
          //       })
          //     } else {//请求失败
          //       wx.hideLoading()
          //     }
          //   },
          //   fail: function (res) { 
          //     wx.hideLoading()
          //   },
          //   complete: function () {
          //     wx.stopPullDownRefresh();
          //   }
          // })
        } else if (reg02.test(result)) {
          console.log("卡片")
          result = result.replace(".html", "")
          var resultSplit = result.split("-")
          if (resultSplit.length == 2) {
            wx.navigateTo({
              url: '/pages/detail/detail?id=' + resultSplit[1],
            })
          } else if (resultSplit.length == 3) {
            wx.navigateTo({
              url: '/pages/detail/detail?id=' + resultSplit[1] + "&islive=true",
              url: '/pages/detail/detail?id=' + resultSplit[1] + "&sid=" + resultSplit[2] + "&islive=true",
            })
          } else {
            wx.showToast({
              title: '错误的二维码',
            })
          }
        } else if (reg03.test(result)) {
          wx.showModal({
            title: '提示',
            content: 'o2o暂不支持二维码扫描',
            showCancel: false
          })
        } else {
          wx.showToast({
            title: '错误的二维码',
          })
        }
      }
    })
  },
  /**
   * 购物车数量
   */
  getCartNum() {
    var that = this
    var url = ""
    var parm = ""
    var token = ""
    var mod = "cartnum"
    console.log("tpuser", getApp().appData.tpuser)
    if (getApp().appData.userInfo) {
      url = getApp().appData.host + "GetUserList"
      token = getApp().appData.userInfo.token
    } else {
      url = getApp().appData.host + "GetList"
      parm = getApp().appData.tpuser
      token = getApp().md5(parm + mod)
    }
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      data: {
        "parm": parm,
        "mod": mod,
        "token": token
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'POST',
      success: function(res) {
        console.log("获取购物车数量", res.data)
        //请求成功
        if (res.data.state == 1) {
          var carNum = res.data.data
          if (carNum > 0 && carNum < 100) {
            wx.setTabBarBadge({
              index: 2,
              text: carNum + "",
            })
          } else if (carNum >= 100) {
            wx.setTabBarBadge({
              index: 2,
              text: "99+",
            })
          } else {
            wx.removeTabBarBadge({
              index: 2,
            })
          }
        } else { //请求失败      

        }
      },
      fail: function(res) {

      },
      complete: function() {

      }
    })
  },
  /**
   * 获取用户信息
   */
  getUser() {
    var that = this
    wx.request({
      url: getApp().appData.host + "GetUser", //仅为示例，并非真实的接口地址
      data: {
        "openid": "",
        "nick": "",
        "unionid": "",
        "type": "1",
        "tpuser": "",
        "plat": "1",
        "rtype": "4",
        "token": getApp().appData.userInfo.token
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'POST',
      success: function(res) {
        console.log("获取用户信息", res.data)
        //请求成功
        if (res.data.state == 1) {
          var renZheng = false;
          if (!res.data.data.mobile) {
            renZheng = true
          }
          that.setData({
            renZheng: renZheng
          })
        } else { //请求失败      

        }
      },
      fail: function(res) {

      },
      complete: function() {

      }
    })
  },
})